<script>
  layui.config({
    base: '/static/customAdmin/',
  }).extend({
    index: 'lib/index',
  }).use(['index'], function () {
    const { table, admin, $ } = layui
    table.render({
      elem: '#role_lists'
      , autoSort: false
      , skin: 'line'
      , method: 'post'
      , where: {
        order: 'desc',
        sort: 'CreatedAt',
      }
      , contentType: 'application/json'
      , url: @{{ urlfor "RoleController.DataGrid" }}
      , request: {
        pageName: 'offset' //页码的参数名称，默认：page
        , limitName: 'limit', //每页数据量的参数名，默认：limit
      }, response: {
        statusName: 'code',
        statusCode: 0,
      }
      , parseData: function (res) {
        return {
          'code': res.code,
          'count': res.total,
          'data': res.rows,
        }
      }
      , cols: [
        [
          { field: 'Name', title: '角色名称', sort: true }
          , { field: 'CreatedAt', title: '创建时间', sort: true }
          , { title: '操作', toolbar: '#role_toolbar', width: 150 },
        ]]
      , page: true
      , limit: 10,
    })

    table.on('sort(role_lists)', function (obj) {
      table.reload('role_lists', {
        initSort: obj
        , where: {
          orderBy: obj.field
          , sortedBy: obj.type,
        },
      })
    })

    $('#role_search').on('keydown', function (e) {
      let eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode
      if (eCode == 13) {
        table.reload('role_lists', {
          where: {
            NameLike: $(this).val(),
          },
          page: {
            curr: 1,
          },
        })
      }
    })

    table.on('tool(role_lists)', function (obj) {
      const data = obj.data
      if (obj.event === 'del') {
        layer.confirm('真的删除么', { title: '提示' }, async (index) => {
          await admin.delete(`/role/delete/${data.Id}`)
          layer.close(index)
          table.reload('role_lists')
        })
      } else if (obj.event === 'edit') {
        const topLayui = parent === self ? layui : top.layui
          , href = `/role/edit?:id=${data.Id}`
          , text = `编辑角色：${data.Name}`
        topLayui.index.openTabsPage(href, text)
      }
    })
  })
</script>